package com.eu.chipin.backend;

import java.util.ArrayList;
import java.util.List;

import com.eu.chipin.R;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.ListAdapter;
import android.widget.ListView;


public class ContactPicker extends Activity {
  private static final Uri _URI = Contacts.CONTENT_URI;
  private static final String[] _ID_NAME = new String[] {
    ContactsContract.Contacts.DISPLAY_NAME,
    ContactsContract.Contacts._ID
  };
  private static final String _HAS_NUM = ContactsContract.Contacts.HAS_PHONE_NUMBER;
  private static final String _SORT_BY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
  private static final int[] views = {android.R.id.text1,android.R.id.text2 };

  private static final int DONE_BTN =  R.id.done_button;;
  private static final int CLEAR_BTN = R.id.clear_button;
  private static final int LIST_VIEW = R.id.listView1;

  private Cursor contacts;

  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    init();
    displayAndSelectContacts();

  }

  private void init()
  {
    setContentView(R.layout.activity_contact_picker);
    ListView listView = (ListView)findViewById(LIST_VIEW);
    DoneListener doneListener = new DoneListener(listView);
    doneListener.setParentActivity(this);
    findViewById(DONE_BTN).setOnClickListener(doneListener);
    findViewById(CLEAR_BTN).setOnClickListener(new ClearListener(listView));

  }

  private class DoneListener implements OnClickListener
  {
    private ListView _listView;
    private Intent result;
    private Activity parentActivity;
    public DoneListener(ListView listView)
    {
      _listView = listView;
    }

    public void setParentActivity(Activity parentActivity)
    {
      this.parentActivity = parentActivity;	
    }
    public void onClick(View view) {
      SparseBooleanArray itemsSelected = _listView.getCheckedItemPositions();
      ArrayList<Member> contacts = new ArrayList<Member>();
      long size = itemsSelected.size();
      for(int index = 0; index < size; index++)
      {
        if(itemsSelected.valueAt(index))
        {
          int pos = itemsSelected.keyAt(index);
          CursorWrapper contact = (CursorWrapper) _listView.getItemAtPosition(pos);
          String name =  contact.getString(0);
          String id = contact.getString(1);
          contacts.add(new Member(id, name, ""));
        }
      }
      finish(contacts);

    }
    private void finish(ArrayList<Member> contacts) {
      if (parentActivity != null) {
        if (contacts != null) {
          if(!contacts.isEmpty()) {
            Intent intent = new Intent();
            intent.putExtra(DataType.ContactsList.toString(), contacts);
            parentActivity.setResult(RESULT_OK, intent);
            parentActivity.finish();
          } else {
            parentActivity.setResult(RESULT_CANCELED);
            parentActivity.finish();
          }
        } else {
          parentActivity.setResult(RESULT_CANCELED);
          parentActivity.finish();
        }
      }
    }

  }

  private class ClearListener implements OnClickListener
  {
    private ListView _listView;

    public ClearListener(ListView listView)
    {
      _listView = listView;
    }
    public void onClick(View view) {
      view.findViewById(LIST_VIEW);
      _listView.clearChoices();
    }

  }

  private Cursor getContacts(ContentResolver contentResolver) {
    return contentResolver.query(_URI, _ID_NAME, _HAS_NUM, null,
                                 _SORT_BY_NAME);
  }



  private void displayAndSelectContacts()
  {
    contacts = getContacts(getContentResolver());
    startManagingCursor(contacts);
    SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this, 
                                                                      android.R.layout.simple_list_item_multiple_choice,
                                                                      contacts,
                                                                      _ID_NAME,
                                                                      views);
    ListView contactList = (ListView)findViewById(R.id.listView1);
    contactList.setAdapter(simpleCursorAdapter);
    contactList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);		

  }



}
